package com.tansaction.controller;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tansaction.config.PageBean;
import com.tansaction.config.R;
import com.tansaction.domain.SysShareholder;
import com.tansaction.service.SysShareholderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;


@RestController
@Slf4j
@RequestMapping("shareholder")
@CrossOrigin
public class ShareholderController {
    @Autowired
    private SysShareholderService shareholderService;

    @RequestMapping("Stortlist")
    public R Stortlist(@RequestBody PageBean pageBean){
        PageHelper.startPage(pageBean);
        QueryWrapper wrapper = new QueryWrapper();
        List<SysShareholder> list = shareholderService.list(wrapper);
        PageInfo<SysShareholder> pageInfo = new PageInfo<>(list);

        return R.successed(pageInfo);
    }

    @RequestMapping("ShareAdd")
    public R ShareAdd(@RequestBody SysShareholder sysShareholder){
        boolean save = shareholderService.save(sysShareholder);

        if (save){
            return R.successed("添加成功");
        }else{
            return R.successed("添加失败");
        }
    }

    @RequestMapping("ShareDelete")
    public R ShareDelete(Integer id){
//        SysShareholder byId = shareholderService.getById(id);
//        byId.setDelFlag(1);
        boolean b = shareholderService.removeById(id);
        if (b){
            return R.successed("删除成功");
        }else {
            return R.successed("删除失败");
        }
    }

    @RequestMapping("ShareUpdate")
    public R ShareUpdate(@RequestBody SysShareholder sysShareholder){
        boolean b = shareholderService.updateById(sysShareholder);

        if (b){
            return R.successed("修改成功");
        }else {
            return R.failed("修改失败");
        }
    }

    @RequestMapping("updateStatus")
    public R updateStatus(@RequestBody SysShareholder sysShareholder){
        shareholderService.updateStatus(sysShareholder);

        if ("0".equals(sysShareholder.getShareholderStatus())){
            return R.successed("状态已改为正常");
        }else if ("1".equals(sysShareholder.getShareholderStatus())){
            return R.successed("状态已改为停用");
        }

        return R.successed(sysShareholder.getShareholderStatus());
    }

    @RequestMapping("getNumber")
    public String getNumber(){
        String s = new Snowflake().nextIdStr();
        return s;
    }





}

